package com.qingcloud.adminbackend.utils;

import org.springframework.util.StringUtils;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class DateUtil {
    public static final String DEFAULT_FORMAT_WITH_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_FORMAT_WITH_DATE_MINUTE = "yyyy-MM-dd HH:mm";
    public static final String DEFAULT_FORMAT_WITH_DATE = "yyyy-MM-dd";
    public static final String DEFAULT_FORMAT_WITH_DATE_2 = "yyyyMMdd";
    public static final String DEFAULT_FORMAT_WITH_HOUR = "yyyy-MM-dd HH";
    public static final String DEFAULT_FORMAT_WITH_TIME = "HH:mm:ss";
    public static final String DEFAULT_FORMAT_DAY_H = "dd-HH";
    public static final String DEFAULT_FORMAT_HOUR = "HH:mm";
    public static final SimpleDateFormat DEFAULT_FORMAT_YMD_HMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DEFAULT_FORMAT_YMD_HM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat DEFAULT_FORMAT_YMD = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat CHINESE_DATE_YMD = new SimpleDateFormat("yyyy年MM月dd日");
    public static final SimpleDateFormat CHINESE_DATE_YM = new SimpleDateFormat("yyyy年MM月");
    public static final SimpleDateFormat CHINESE_DATE_Y = new SimpleDateFormat("yyyy年");
    public static final DateTimeFormatter CHINESE_DATE_YMD_2 = DateTimeFormatter.ofPattern("yyyyMMdd");

    public static String getCurrentDateTime() {
        return DEFAULT_FORMAT_YMD_HMS.format(Calendar.getInstance().getTime());
    }

    public static String getCurrentDateTime(String pattern) {
        if (CommonUtil.isEmpty(pattern)) {
            return getCurrentDateTime();
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(Calendar.getInstance().getTime());
        }
    }

    public static String getCurrentHourTime(Date date, String fmt) {
        SimpleDateFormat sdf1 = new SimpleDateFormat(fmt);
        return sdf1.format(date);
    }

    public static String getPreHourTime(Date date, String fmt) {
        SimpleDateFormat sdf1 = new SimpleDateFormat(fmt);
        Date repHour = addHours(date, -1);
        return sdf1.format(repHour);
    }

    public static final String strDateFormat(Date date) {
        if (date != null) {
            return DEFAULT_FORMAT_YMD_HMS.format(date);
        } else {
            return "";
        }
    }

    public static final String strDateFormat(String date, String pattern) throws Exception {
        if (date != null && pattern != null) {
            SimpleDateFormat sdf1 = new SimpleDateFormat(pattern);
            Date d = sdf1.parse(date);
            return sdf1.format(d);
        } else {
            return "";
        }
    }

    public static String strDateAddOrDecHours(String date, String pattern, int hours) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(date));
        int hour = cal.get(11);
        cal.set(11, hour + hours);
        return (new SimpleDateFormat(pattern)).format(cal.getTime());
    }

    public static String strDateAddOrDecDays(String date, String pattern, int days) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime((new SimpleDateFormat("yyyy-MM-dd")).parse(date));
        int day = cal.get(5);
        cal.set(5, day + days);
        return (new SimpleDateFormat(pattern)).format(cal.getTime());
    }

    public static Date getQoQ(Date current, int field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(current);
        calendar.add(field, -1);
        return calendar.getTime();
    }

    public static String getDateTime(Date date, String pattern) {
        if (CommonUtil.isEmpty(pattern)) {
            return getCurrentDateTime("yyyy-MM-dd HH:mm:ss");
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(date);
        }
    }

    public static String getDateTime(Date date) {
        return DEFAULT_FORMAT_YMD_HMS.format(date);
    }

    public static String getDateTime(long date, String pattern) {
        if (CommonUtil.isEmpty(pattern)) {
            return DEFAULT_FORMAT_YMD_HMS.format(Long.valueOf(date));
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(Long.valueOf(date));
        }
    }

    public static String getDateTime(long date) {
        return DEFAULT_FORMAT_YMD_HMS.format(Long.valueOf(date));
    }

    public static String getCurrentDate() {
        return DEFAULT_FORMAT_YMD.format(Calendar.getInstance().getTime());
    }

    public static String getCurrentTime() {
        return DEFAULT_FORMAT_YMD_HMS.format(Calendar.getInstance().getTime());
    }

    public static int getCurrentYear() {
        return Calendar.getInstance().get(1);
    }

    public static int getCurrentModth() {
        return Calendar.getInstance().get(2) + 1;
    }

    public static int getCurrentDay() {
        return Calendar.getInstance().get(5);
    }

    public static Date parse(String date) {
        Date dateTime = null;

        try {
            dateTime = DEFAULT_FORMAT_YMD_HMS.parse(date);
        } catch (ParseException arg2) {
            arg2.printStackTrace();
        }

        return dateTime;
    }

    public static Date parse(Date date, String parse) {
        String dateTime = getDateTime(date, parse);
        return parse(dateTime, parse);
    }

    public static Date parse(String date, String pattern) {
        try {
            if (CommonUtil.isEmpty(pattern)) {
                return DEFAULT_FORMAT_YMD_HMS.parse(date);
            } else {
                SimpleDateFormat e = new SimpleDateFormat(pattern);
                return e.parse(date);
            }
        } catch (ParseException arg2) {
            arg2.printStackTrace();
            return null;
        }
    }

    public static Date getTime() {
        return Calendar.getInstance().getTime();
    }

    public static Date getDateFromChineseFormat(String strDate) {
        Date date = new Date();

        try {
            date = CHINESE_DATE_YMD.parse(strDate);
        } catch (ParseException arg2) {
            arg2.printStackTrace();
        }

        return date;
    }

    public static Date getDateFromChineseFormatYM(String strDate) {
        Date date = new Date();

        try {
            date = CHINESE_DATE_YM.parse(strDate);
        } catch (ParseException arg2) {
            arg2.printStackTrace();
        }

        return date;
    }

    public static Date getDateFromChineseFormatY(String strDate) {
        Date date = new Date();

        try {
            date = CHINESE_DATE_Y.parse(strDate);
        } catch (ParseException arg2) {
            arg2.printStackTrace();
        }

        return date;
    }

    public static Date getDateOfYearEnd(String strDate) {
        Calendar calendar = Calendar.getInstance();
        new Date();

        try {
            Date date = CHINESE_DATE_Y.parse(strDate);
            calendar.setTime(date);
            calendar.add(2, 11);
        } catch (ParseException arg3) {
            arg3.printStackTrace();
        }

        calendar.set(5, calendar.getActualMaximum(5));
        return calendar.getTime();
    }

    public static Date getSeasonStartDate(String strDate) {
        Calendar calendar = Calendar.getInstance();
        String year = "2017";
        String season = "1";
        if (CommonUtil.isNotEmpty(strDate)) {
            year = strDate.substring(0, 5);
            season = strDate.substring(5, 6);
        }

        Date startDate = getDateFromChineseFormatY(year);
        calendar.setTime(startDate);
        if ("2".equals(season)) {
            calendar.add(2, 3);
        } else if ("3".equals(season)) {
            calendar.add(2, 6);
        } else if ("4".equals(season)) {
            calendar.add(2, 9);
        }

        return calendar.getTime();
    }

    public static Date getSeasonEndDate(String strDate) {
        Calendar calendar = Calendar.getInstance();
        String year = "2017";
        String season = "1";
        if (CommonUtil.isNotEmpty(strDate)) {
            year = strDate.substring(0, 5);
            season = strDate.substring(5, 6);
        }

        Date startDate = getDateFromChineseFormatY(year);
        calendar.setTime(startDate);
        if ("1".equals(season)) {
            calendar.add(2, 2);
        } else if ("2".equals(season)) {
            calendar.add(2, 5);
        } else if ("3".equals(season)) {
            calendar.add(2, 8);
        } else if ("4".equals(season)) {
            calendar.add(2, 11);
        }

        calendar.set(5, calendar.getActualMaximum(5));
        return calendar.getTime();
    }

    public static String getBeginOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return getDateTime(calendar.getTime());
    }

    public static String getEndOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        return getDateTime(calendar.getTime());
    }

    public static String getStartOfDay(String date) {
        Date parseDate = parse(date, "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseDate);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return getDateTime(calendar.getTime());
    }

    public static long getMillis(Calendar cal) {
        return cal.getTime().getTime();
    }


    public static String getEndOfDay(String date) {
        Date parseDate = parse(date, "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseDate);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        return getDateTime(calendar.getTime());
    }

    public static String[] getDateBetween(Date fromDate, int days) {
        Calendar toCalendar = Calendar.getInstance();
        toCalendar.add(6, days);
        Date toDate = toCalendar.getTime();
        if (fromDate.getTime() > toDate.getTime()) {
            Date tempDate = fromDate;
            fromDate = toDate;
            toDate = tempDate;
        }

        return new String[]{getDateTime(fromDate, "yyyy-MM-dd"), getDateTime(toDate, "yyyy-MM-dd")};
    }

    public static String[] getDateBetween(int days) {
        Date fromDate = new Date();
        return getDateBetween(fromDate, days);
    }

    public static Date addHours(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(11, x);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static String addHours(String time, int hour) throws Exception {
        if (StringUtils.isEmpty(time)) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT_WITH_DATE_TIME);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(time));
            calendar.add(Calendar.HOUR_OF_DAY, hour);
            return sdf.format(calendar.getTime());
        }
    }

    public static Date addMinute(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(12, x);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }


    public static String[] getMonthFirstAndLastDay(String dateStr, String fmt) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        Date date = sdf.parse(dateStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(2);
        int year = cal.get(1);
        System.out.println(year);
        cal.set(1, year);
        cal.set(2, month);
        cal.set(5, cal.getMinimum(5));
        String first = (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
        cal.set(5, cal.getActualMaximum(5));
        String last = (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
        return new String[]{first + " 00:00:00", last + " 23:59:59"};
    }

    public static int getYearOfDate(String dateStr, String fmt) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        Date date = sdf.parse(dateStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(1);
    }

    public static Integer getMontDays(String strDate, String fmt) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        Date date = sdf.parse(strDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(5, 1);
        cal.add(2, 1);
        cal.add(5, -1);
        int days = cal.get(5);
        return Integer.valueOf(days);
    }

    public static XMLGregorianCalendar dateToXmlDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DatatypeFactory dtf = null;

        try {
            dtf = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException arg3) {
            ;
        }

        XMLGregorianCalendar dateType = dtf.newXMLGregorianCalendar();
        dateType.setYear(cal.get(1));
        dateType.setMonth(cal.get(2) + 1);
        dateType.setDay(cal.get(5));
        dateType.setHour(cal.get(11));
        dateType.setMinute(cal.get(12));
        dateType.setSecond(cal.get(13));
        return dateType;
    }

    public static Date xmlDate2Date(XMLGregorianCalendar cal) {
        try {
            return cal.toGregorianCalendar().getTime();
        } catch (Exception arg1) {
            return null;
        }
    }

    public static Long getDateHourPoor(Date endDate, Date startDate) {
        long nh = 3600000L;
        long diff = endDate.getTime() - startDate.getTime();
        long hour = diff / nh;
        return Long.valueOf(hour);
    }

    public static Date addDay(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(5, x);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static Date addClearHours(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(11, x);
            cal.set(13, 0);
            cal.set(12, 0);
            cal.set(14, 0);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static Date addClearDay(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(5, x);
            cal.set(11, 0);
            cal.set(13, 0);
            cal.set(12, 0);
            cal.set(14, 0);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static Date addMonth(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(2, x);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static Date addClearMonth(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(2, x);
            cal.add(5, -1);
            cal.set(11, 0);
            cal.set(13, 0);
            cal.set(12, 0);
            cal.set(14, 0);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static Date addYear(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(1, x);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static Date addClearYear(Date date, int x) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(1, x);
            cal.set(2, 0);
            cal.set(5, 0);
            cal.set(11, 0);
            cal.set(13, 0);
            cal.set(12, 0);
            cal.set(14, 0);
            date = cal.getTime();
            cal = null;
            return date;
        }
    }

    public static List<String> listHourTimeGroupByTime(String startTime, String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(CommonUtil.DATAFORMAT_SS);
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        startCalendar.setTime(sdf.parse(startTime));
        endCalendar.setTime(sdf.parse(endTime));
        List<String> timeList = new ArrayList<>();

        while (startCalendar.compareTo(endCalendar) <= 0) {
            timeList.add(sdf.format(startCalendar.getTime()));
            startCalendar.add(Calendar.HOUR_OF_DAY, 1);
        }
        return timeList;
    }


    public static List<Object> getDateHourBetweenTwoDate2(String startTime, String endTime) {
        List<Object> list = new ArrayList<>();
        Date startDate = parse(startTime, DEFAULT_FORMAT_WITH_DATE);
        Date endDate = parse(endTime, DEFAULT_FORMAT_WITH_DATE);

        String[] hours = getMy24Hours(getDateTime(startDate, DEFAULT_FORMAT_WITH_DATE));
        // 把开始时间加入集合
        Collections.addAll(list, hours);

        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(startDate);
        Boolean flag = true;
        while (flag) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date temp = cal.getTime();
            // 测试此日期是否在指定日期之后
            if (endDate.after(temp)) {
                hours = getMy24Hours(getDateTime(temp, DEFAULT_FORMAT_WITH_DATE));
                Collections.addAll(list, hours);
            } else {
                flag = false;
                break;
            }
        }
        hours = getMy24Hours(getDateTime(endDate, DEFAULT_FORMAT_WITH_DATE));
        Collections.addAll(list, hours);
        return list;
    }

    /**
     * TODO What the method does
     *
     * @param statKey
     * @return
     * @author liuyulong
     * @date 2016年12月19日-下午5:53:07
     */
    private static String[] getMy24Hours(String statKey) {
        String[] hours = new String[]{"00:00:00", "01:00:00", "02:00:00", "03:00:00", "04:00:00", "05:00:00", "06:00:00", "07:00:00", "08:00:00",
                "09:00:00", "10:00:00", "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00", "19:00:00",
                "20:00:00", "21:00:00", "22:00:00", "23:00:00"};
        for (int i = 0; i < hours.length; i++) {
            hours[i] = statKey + " " + hours[i];
        }

        return hours;
    }

    /**
     * 获取两个时间字串之前的 所有 小时集合
     *
     * @param startTime
     * @param endTime
     * @return
     * @author liuyulong
     * @date 2016年12月15日-下午3:30:35
     */
    public static Set<Object> getDatesBetweenTwoDate(String startTime, String endTime) {
        Date beginDate = parse(startTime, DEFAULT_FORMAT_WITH_DATE_TIME);
        Date endDate = parse(endTime, DEFAULT_FORMAT_WITH_DATE_TIME);
        Set<Object> lDate = new LinkedHashSet<Object>();
        lDate.add(getDateTime(beginDate));// 把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(beginDate);
        Boolean flag = true;
        while (flag) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.HOUR_OF_DAY, 1);
            Date temp = cal.getTime();
            // 测试此日期是否在指定日期之后
            if (endDate.after(temp)) {
                lDate.add(getDateTime(temp));
            } else {
                flag = false;
                break;
            }
        }
        lDate.add(getDateTime(endDate));// 把结束时间加入集合
        return lDate;
    }

    public static String getTime(String date, String pattern) {
        if (CommonUtil.isEmpty(date)) {
            return date;
        }
        if (CommonUtil.isEmpty(pattern)) {
            return DEFAULT_FORMAT_YMD_HMS.format(date);
        } else {
            Date parse = null;
            try {
                parse = DEFAULT_FORMAT_YMD_HM.parse(date);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            SimpleDateFormat e = new SimpleDateFormat(pattern);
            return e.format(parse);
        }
    }


    /**
     * 获取天时间范围yyyy-MM-dd
     */
    public static List<String> listDayRange(String startDate, String endDate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(CommonUtil.DATAFORMAT);
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        startCalendar.setTime(sdf.parse(startDate));
        endCalendar.setTime(sdf.parse(endDate));
        List<String> dateList = new ArrayList<>();

        while (startCalendar.compareTo(endCalendar) <= 0) {
            dateList.add(sdf.format(startCalendar.getTime()));
            startCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return dateList;
    }


    /**
     * 获取小时时间范围00:00-23:00
     */
    public static List<String> list24Hour() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(CommonUtil.DATAFORMAT_HM);
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        startCalendar.setTime(sdf.parse("00:00"));
        endCalendar.setTime(sdf.parse("23:00"));
        List<String> timeList = new ArrayList<>();

        while (startCalendar.compareTo(endCalendar) <= 0) {
            timeList.add(sdf.format(startCalendar.getTime()));
            startCalendar.add(Calendar.HOUR_OF_DAY, 1);
        }
        return timeList;
    }

    /**
     * 获取小时数
     */
    public static Integer getHour(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT_HOUR);
        try {
            return sdf.parse(time).getHours();
        } catch (Exception e) {
            return null;
        }

    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        if (day > 0) {
            return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
        }
        if (hour > 0) {
            return hour + "小时" + min + "分钟" + sec + "秒";
        }
        if (min > 0) {
            return min + "分钟" + sec + "秒";
        } else {
            return sec + "秒";
        }
    }

    public static String getDate(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        return hour + ":" + min + ":" + sec;
    }

    /**
     * 获取今天星期几
     *
     * @return
     */
    public static String getWeekDay() {
        //获取当前星期几
        Calendar calendar;
        calendar = Calendar.getInstance();
        String week;
        week = calendar.get(calendar.DAY_OF_WEEK) - 1 + "";
        if ("0".equals(week)) {
            week = "7";
        }
        return week;
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
}
